package csputil

import (
	"encoding/base64"
	"encoding/pem"

	"github.com/pkg/errors"
)

func ExtractCaCertPemBlock(caCertBase64 string) (*pem.Block, error) {
	var err error
	var decoded []byte
	if caCertBase64 != "" {
		decoded, err = base64.URLEncoding.DecodeString(caCertBase64)
		if err != nil {
			return nil, err
		}
		b, _ := pem.Decode(decoded)
		return b, nil
	} else {
		return nil, errors.New("ca cert is empty")
	}
}
